package com.zhb.tools

import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.FormatStrategy
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy

/**
 * 皇包车Log日志
 * Created by ZHZEPHI on 2017-01-09.
 */
object ZLog {
    private var LOG_TAG = "ZLog"
    private var ISDEBUG = true //默认打开日志
    private var IS_SHOW_THREAD = false //默认不显示线程信息
    private var METHOD_COUNT = 3 //方法显示层级
    private var METHOD_OFFSET = 2 //隐藏内部方法调用
    private fun init() {
        Logger.clearLogAdapters()
        val formatStrategy: FormatStrategy = PrettyFormatStrategy.newBuilder()
            .showThreadInfo(IS_SHOW_THREAD) // (Optional) Whether to show thread info or not. Default true
            .methodCount(METHOD_COUNT) // (Optional) How many method line to show. Default 2
            .methodOffset(METHOD_OFFSET) // (Optional) Hides internal method calls up to offset. Default 5
            //                .logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
            .tag(LOG_TAG) // (Optional) Global tag for every log. Default PRETTY_LOGGER
            .build()
        Logger.addLogAdapter(AndroidLogAdapter(formatStrategy))
    }

    /**
     * 打印调试信息
     *
     * @param msg
     */
    fun d(msg: String?) {
        if (ISDEBUG) {
            Logger.d(msg)
        }
    }

    /**
     * 打印信息
     *
     * @param msg
     */
    fun i(msg: String?) {
        if (ISDEBUG) {
            Logger.i(msg!!)
        }
    }

    /**
     * 打印错误
     *
     * @param msg
     */
    fun e(msg: String?) {
        if (ISDEBUG) {
            Logger.e(msg!!)
        }
    }

    /**
     * 支持Throwable打印
     *
     * @param msg
     * @param throwable
     */
    fun e(msg: String?, throwable: Throwable?) {
        if (ISDEBUG) {
            Logger.e(throwable, msg!!)
        }
    }

    /**
     * 打印json
     *
     * @param msg
     */
    fun json(msg: String?) {
        if (ISDEBUG) {
            Logger.json(msg)
        }
    }

    /**
     * 打印xml
     *
     * @param msg
     */
    fun xml(msg: String?) {
        if (ISDEBUG) {
            Logger.xml(msg)
        }
    }

    /**
     * 设置调试模式
     * 只有为true才会打印日志
     *
     * @param isDebug
     */
    fun setIsDebug(isDebug: Boolean) {
        ISDEBUG = isDebug
    }

    /**
     * 设置日志TAG标记
     *
     * @param logTag
     */
    fun setLogTag(logTag: String) {
        LOG_TAG = logTag
        init()
    }

    fun setIsShowThread(isShowThread: Boolean) {
        IS_SHOW_THREAD = isShowThread
        init()
    }

    /**
     * 设置方法层数
     *
     * @param methodCount
     */
    fun setMethodCount(methodCount: Int) {
        METHOD_COUNT = methodCount
        init()
    }

    fun setMethodOffset(methodOffset: Int) {
        METHOD_OFFSET = methodOffset
        init()
    }

    init {
        init()
    }
}